/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.chart.attachment.chartattachment;

import java.awt.Color;
import java.awt.Point;
import java.beans.PropertyChangeEvent;

import twaver.TDataBox;
import twaver.TUIManager;
import twaver.TWaverConst;
import twaver.TWaverUtil;
import twaver.chart.AbstractChart;
import twaver.network.ui.ComponentAttachment;
import twaver.network.ui.ElementUI;
import demo.chart.attachment.ChartAttachmentDemo;

public abstract class ElementAttachment extends ComponentAttachment {

	public final static String CHARTBOX = "elementattachment.chartbox";
	public final static String PIEATTACHMENT = "elementattachment.pie";
	public final static String DIALATTACHMENT = "elementattachment.dial";
	public final static String PERCENTATTACHMENT = "elementattachment.percent";
	public final static String RADARATTACHMENT = "elementattachment.radar";
	public final static String BARATTACHMENT = "elementattachment.bar";
	public final static String LINEATTACHMENT = "elementattachment.line";
	public final static String BUBBLEATTACHMENT = "elementattachment.bubble";

	public final static String WIDTH = "attachment.width";
	public final static String HEIGHT = "attachment.height";
	public final static String LEGENDVISIBLE = "legendvisible";

	public static int AttachmentWidth = 120;
	public static int AttachmentHeight = 80;

	protected AbstractChart chart;

	public ElementAttachment(String name, ElementUI ui) {
		super(name, ui);
		initAttachment();
	}

	public ElementAttachment(String name, ElementUI ui, boolean minimized, boolean shrinked) {
		super(name, ui, minimized, shrinked);
		initAttachment();
	}

	private void initAttachment() {
		this.setPosition(TWaverConst.POSITION_CENTER);
		this.setWidth(AttachmentWidth);
		this.setHeight(AttachmentHeight);
		initChart();
	}

	protected abstract void initChart();

	protected void setChartProperty(AbstractChart chart) {
		chart.setOpaque(false);
		chart.setBackgroundVisible(true);
		chart.getLegendPane().setVisible(false);
		Object chartBox = element.getClientProperty(CHARTBOX);
		if (chartBox != null && chartBox instanceof TDataBox) {
			chart.setDataBox((TDataBox) chartBox);
		}
		this.chart = chart;
		chart.setValueTextColor(Color.BLUE);
		chart.setValueTextFont(TUIManager.getDefault10SizeFont());
		element.setBusinessObject(chart);
		this.setComponent(chart);
		this.setAlpha(0.9f);
	}

	public void elementPropertyChange(PropertyChangeEvent evt) {
		if (TWaverConst.PROPERTYNAME_SELECTED.equals(TWaverUtil.getPropertyName(evt))) {
			if (element.isSelected()) {
				chart.setBackgroundFillColor(Color.GREEN.darker());
				this.setWidth((int) (AttachmentWidth * 1.35));
				this.setHeight((int) (AttachmentHeight * 1.35));
				this.setAlpha(1f);
			}
			else {
				chart.setBackgroundFillColor(TWaverConst.COLOR_GRAY);
				this.setWidth(AttachmentWidth);
				this.setHeight(AttachmentHeight);
				this.setAlpha(0.9f);
			}
		}
		else if (LEGENDVISIBLE.equals(TWaverUtil.getPropertyName(evt))) {
			Boolean visible = (Boolean) evt.getNewValue();
			chart.getLegendPane().setVisible(visible.booleanValue());
		}
		else if (TWaverConst.PROPERTYNAME_LOCATION.equals(TWaverUtil.getPropertyName(evt))) {
			if (!this.element.isSelected()) {
				Point mouseLocation = ChartAttachmentDemo.MOUSELOCATION;
				if (mouseLocation != null && network.getElementPhysicalAt(mouseLocation.x, mouseLocation.y) == this.element) {
					chart.setBackgroundFillColor(new Color(100, 100, 0));
					this.setWidth((int) (AttachmentWidth * 1.2));
					this.setHeight((int) (AttachmentHeight * 1.2));
					this.setAlpha(1f);
				}
				else {
					chart.setBackgroundFillColor(TWaverConst.COLOR_GRAY);
					this.setWidth(AttachmentWidth);
					this.setHeight(AttachmentHeight);
					this.setAlpha(0.9f);
				}
			}
			else {
				this.setWidth((int) (AttachmentWidth * 1.35));
				this.setHeight((int) (AttachmentHeight * 1.35));
			}
		}
		super.elementPropertyChange(evt);
	}
}